Skip to content

Hid/DFU boot loader (superseded by #525) #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed

Hid/DFU boot loader (superseded by #525) #415

wants to merge 4 commits into from

Conversation

BennehBoy
Copy link
Contributor

@BennehBoy BennehBoy commented Jan 21, 2019

This PR enables the use of the STM32 HID Bootloader 3.0 for Generic F103 & Generic F4 variants

Tested on Bluepill, Blackpill (using BP menu option), & Black407VET6.

Limitations:

Usage:

First apply this PR to your STM32 tools -> stm32duino/Arduino_Tools#33.

HID:
Select your variant in the boards menu
Set the upload method to "HID Bootloader 3.0 (0x0800)" for F1 or "HID Bootloader 3.0 (0x4000)" for F4
Ensure that "USB Interface (if available):" is set to "CDC Full Speed" (required for DTR toggling)

DFU:
The PR applies the magic word & byte code recognition to USB_CDC_IF.cpp which also enables STM32Duino Bootloader 2.0 to work. Also included is DISC pin handling for Maple Mini. This is the same logic used in Roger Clarke's core and in STM32GENERIC
Select your variant in the boards menu
Set the upload method to "Maple DFU Bootloader 2.0" or "Maple DFU Bootloader original (0x5000)" depedning on the BL installed.
Ensure that "USB support (if available):" is set to either CDC option (required for DTR toggling)

Note - edited due to changes to HID Bootloader 3.0 on 21/1/19. & addition of DFU 27/1/2019

@BennehBoy BennehBoy mentioned this pull request Jan 21, 2019
@BennehBoy
Copy link
Contributor Author

BennehBoy commented Jan 21, 2019

Had to build new binaries from of the HID bootloader to set the correct GPIO for BOOT1 to fix the boot into sketch issue, as it stood the source code was using a pin that would be floating.

Here's what I used for Black407Vet6 (/inc/main.h):

#define BOOT_1_PIN	GPIO_PIN_2 //Blackboard 407VET
#define BOOT_1_PORT	GPIOB
#define LED_1_PIN		GPIO_PIN_6
#define LED_1_PORT	GPIOA

And for Black407ZET6

#define BOOT_1_PIN	GPIO_PIN_2 //Blackboard 407ZET
#define BOOT_1_PORT	GPIOB
#define LED_1_PIN		GPIO_PIN_10
#define LED_1_PORT	GPIOF

ZET6 now tested and working.

Will test DIYMROE next.

@RickKimball
Copy link
Contributor

You probably want to adjust the total available flash:

#ifdef HIDBL_F1
FLASH (rx)      : ORIGIN = 0x8000800, LENGTH = 64K
#else

might be:

#ifdef HIDBL_F1
FLASH (rx)      : ORIGIN = 0x8000800, LENGTH = 62K
#else

goes for all .ld scripts

@BennehBoy
Copy link
Contributor Author

@RickKimball yeah very good call, will update.

@BennehBoy
Copy link
Contributor Author

I've made the linker changes for all but BLACK407XX, the MAX size is passed in as a preprocessor variable - been at the gym so my brain is struggling to think how to knock 16KB off a preprocessed variable, or set it in boards.txt only for HID method?

@fpistm
Copy link
Member

fpistm commented Jan 21, 2019

Tips: you can define new switch and use it in ld script. Ex: LD_SIZE_OFFSET

@BennehBoy
Copy link
Contributor Author

Think I found a simpler way:

FLASH (rx) : ORIGIN = 0x8004000, LENGTH = LD_MAX_SIZE - 0x4000

Seems to work - ie doesn't throw linker error

@BennehBoy
Copy link
Contributor Author

I've left out the change to the upload method for now since 1) it works as is (no idea why) 2) it doesn't work when the correct naming is used.

@BennehBoy
Copy link
Contributor Author

I've attached precompiled bootloader bins, labelled with the boards they match.

firmwares.zip

@edogaldo
Copy link
Contributor

What's the advantage of using this bootloader vs using the DFU versions?

Thanks in advance, E.

@uzi18
Copy link

uzi18 commented Jan 23, 2019

  • not need to shuffle with jumpers
  • can jump from user code to bootloader automatically
  • no use of any drivers during upload

something more?

@BennehBoy
Copy link
Contributor Author

Pretty much that.

@BennehBoy
Copy link
Contributor Author

I'll upload a different formware for zet6 later, BOOT1 is pulled down on that board and it needs to be pulled up for this BL (so as an alternate it can be compiled to use PD4 which is an SDIO pin with a pullup... won't be active when the BL is running).

@uzi18
Copy link

uzi18 commented Jan 23, 2019

maybe just try to read K0 or K1

@BennehBoy
Copy link
Contributor Author

After seeing the above ref, I started to play with adding maple DFU support.

I can successfully upload to a maple mini with the original 0x5000 dfu bootloader - linking works well. BUT, CDC does not function - not sure why.

Will test against Bluepill & Blackpill

@fpistm
Copy link
Member

fpistm commented Jan 24, 2019

@BennehBoy on Maple do not forget the DISC pin PB9 handling. I will add the DISC pin management based on what is done in Roger's core.

@BennehBoy
Copy link
Contributor Author

@fpistm of course...

I had a quick look through the code but the LL stuff is beyond me at present...

Here's what STM32GENERIC does:


void USBDeviceClass::reenumerate() {
    /* Re-enumerate the USB */
      volatile unsigned int i;

    #ifdef USB_DISC_PIN
      pinMode(USB_DISC_PIN, OUTPUT);
      digitalWrite(USB_DISC_PIN, HIGH);
        for(i=0;i<512;i++);
      digitalWrite(USB_DISC_PIN, LOW);
    #else
      //pinMode(USBDP_PIN, OUTPUT);
      //digitalWrite(USBDP_PIN, LOW);
        //for(i=0;i<512;i++);
      //digitalWrite(USBDP_PIN, HIGH);


      pinMode(PA12, OUTPUT);
      digitalWrite(PA12, LOW);
      //HAL_Delay(1000);
      for(i=0;i<1512;i++){};
      pinMode(PA12, INPUT);
      //digitalWrite(PA12, HIGH);
      //HAL_Delay(1000);
      for(i=0;i<512;i++){};
    #endif
}

reenumerate gets called when CDC begins.

I tried 'frigging' it by issuing the same code in sketch startup, but CDC has already started at that point and things don'tr go well.

On a side note, Bluepill seems to get the BL 2.0 overwritten even though the linker & vector offset are 0x2000

@BennehBoy
Copy link
Contributor Author

OK, fixed bluepill...

So have working DFU BL 2.0 for BP 😃

Just need the DISC pin handling to get Maple Mini working

@BennehBoy
Copy link
Contributor Author

I still can't figure out why anything other than bmpMethod fails for the naming in boards.txt

@BennehBoy BennehBoy changed the title Hid boot loader Hid/DFU boot loader Jan 24, 2019
@BennehBoy
Copy link
Contributor Author

I've pushed the DFU changes - Bluepill functional, Maple Mini semi functional but needs the USB_DISC_PIN change.

@BennehBoy
Copy link
Contributor Author

BennehBoy commented Jan 24, 2019

I think the DISC_PIN handling needs inserting here -> https://github.com/stm32duino/Arduino_Core_STM32/blob/master/cores/arduino/stm32/usb/usbd_conf.c#L58

I'm struggling a bit on the LL code to initialise and toggle the pin though.

@BennehBoy
Copy link
Contributor Author

BennehBoy commented Jan 24, 2019

Here's what I came up with, I used HAL... It works, but may not be the best way. This inserted just before the line indicated above.

It's hard coded for PB9 at present - but tbh I think there's only Maple Mini that uses this.

	/* BEGIN USER CODE */
	#ifdef USB_DISC_PIN
        uint16_t i=0;
	GPIO_InitTypeDef GPIO_InitStruct = {0};
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
	GPIO_InitStruct.Pin = GPIO_PIN_9;
	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
	HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
        for(i=0;i<512;i++);
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
        #endif
	
	/* END USER CODE */

@BennehBoy
Copy link
Contributor Author

Works a charm with #410

@fpistm
Copy link
Member

fpistm commented Jan 24, 2019

I will provide a PR tomorrow based on #410 with USB DISC PIN management and reenumerate

@uzi18
Copy link

uzi18 commented Jan 24, 2019

just in case, please add #undefine for HiGH and LOW before #endif

@BennehBoy
Copy link
Contributor Author

just in case, please add #undefine for HiGH and LOW before #endif

I removed those anyway - was a leftover from before conversion to HAL.

@ag88
Copy link
Contributor

ag88 commented Apr 8, 2019

hi no worries, i think cubeprogrammer, DFU, HID can co-exist in a sense it is kind of a variant where the start of flash and vtab address is different that's all. in terms of tools, i'd think it's alright for ST to make a decision. users can always use 'external' tools like dfu-util or hid-flash to install the sketch say on the command line separately. or some instructions in a wiki can be placed to show how to add the tools. so non issues :)

@rtek1000
Copy link

Hi,

In case the user reaches this repository (usage instructions):
Serasidis/STM32_HID_Bootloader#11

@chrissbarr chrissbarr mentioned this pull request May 1, 2019
2 tasks
@BennehBoy
Copy link
Contributor Author

@fpistm I noticed the 1.6.0 milestone, does this mean you're refactoring with stmcubeprogrammer?

@fpistm
Copy link
Member

fpistm commented May 3, 2019

@fpistm I noticed the 1.6.0 milestone, does this mean you're refactoring with stmcubeprogrammer?

I was wondering how long you would take to ask 😄
So, yes, I will review and test it after rebase/refactor it properly.

@BennehBoy
Copy link
Contributor Author

🤣

@uzi18
Copy link

uzi18 commented May 3, 2019

what about hid bootloader/programmer? it is not supported by stmcubeprogrammer.

@fpistm
Copy link
Member

fpistm commented May 3, 2019

I will add it

@fpistm
Copy link
Member

fpistm commented May 16, 2019

@BennehBoy, @Serasidis
I'm wondering if we should not keep only HID bootloader?
Is Maple DFU Bootloader 2.0 and Maple DFU Bootloader original (0x5000) really needed?
What is your thought about that?

@BennehBoy
Copy link
Contributor Author

BennehBoy commented May 16, 2019 via email

@fpistm
Copy link
Member

fpistm commented May 16, 2019

Right, forgot this use case.
Thanks for the quick answer ;)

@fpistm
Copy link
Member

fpistm commented May 17, 2019

@BennehBoy

* F1 boards must use the latest 2K HID bootloader -> https://github.com/Serasidis/STM32_HID_Bootloader  (hid_flash tool will warn if an older version is installed)

I'm little bit lost with that. I know some users works on this but I think it was not the case here as it is not compatible with EEPROM emulation.
And reffering to your updated ld the BL is a the start of the flash.

@BennehBoy
Copy link
Contributor Author

Can you explain further?

When I was creating the PR the HID BL author updated his repo so that the tools would refuse to flash devices with an earlier version of the BL installed....

Then some more commits were made to move the F1 version of the BL to the end of flash, these were then backed out.

I've noticed some recent commits but have not had my eye close enough on it to see if the end of flash has been re-introduced (I doubt it would be due to some other issues).

@fpistm
Copy link
Member

fpistm commented May 17, 2019

Ok so your limitation on your OP is not correct.
Currently, the bootloader is flashed at the beginning?

@BennehBoy
Copy link
Contributor Author

BennehBoy commented May 17, 2019

The limitation is correct with the hid tool that I supplied in the tools repo. Where the loader is flashed in flash memory is irrelevant, the tool still requires v 2+ to be on the device.

@fpistm
Copy link
Member

fpistm commented May 17, 2019

Oh ok. Sorry, i've read that it was flash on the latest sector 🤣
So, I've clen up your PR (split commit, clean code, enhance boards.txt, add switch to enable it only for compatible series, use backup API, ...)
See #525

@neilbalch
Copy link

Out of curiosity, could one of you please explain what is currently hindering this PR from being merged in relatively lay-person parlance? From what this looks to be, It would be of great use to me to have this feature in the STM32duino core.

@fpistm fpistm changed the title Hid/DFU boot loader Hid/DFU boot loader (superseded by #525) May 20, 2019
@fpistm
Copy link
Member

fpistm commented May 20, 2019

Out of curiosity, could one of you please explain what is currently hindering this PR from being merged in relatively lay-person parlance? From what this looks to be, It would be of great use to me to have this feature in the STM32duino core.

@neilbalch
This PR is now superseded by #525
This will be merged soon.
@BennehBoy made a great job on this PR. It only required some time to review, test and merge.

@fpistm fpistm added abandoned No more work on this and removed Review on going labels May 20, 2019
@BennehBoy
Copy link
Contributor Author

@fpistm I figure you've guessed my time has been limited lately - the original reason I got involved with microcontrollers, my car, has stolen most of my recent time... Engine rebuilds, gearbox work etc. Never buy a Land Rover!

I'll try and get some testing of PR #525 done soon.

@BennehBoy BennehBoy closed this May 20, 2019
@BennehBoy BennehBoy deleted the HIDBootLoader branch May 20, 2019 09:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
abandoned No more work on this enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants